package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapp.note.DBUtils;
import com.example.myapp.note.SQLiteHelper;

public class RecordActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView ivBack, ivDelete, ivSave;
    TextView tvNoteTime, tvAction;
    EditText etContent;
    SQLiteHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        etContent = (EditText) findViewById(R.id.etContent);
        ivBack = (ImageView) findViewById(R.id.ivBack);
        ivDelete = (ImageView) findViewById(R.id.ivDelete);
        ivSave = (ImageView) findViewById(R.id.ivSave);
        ivBack.setOnClickListener(this);
        ivDelete.setOnClickListener(this);
        ivSave.setOnClickListener(this);
        helper = new SQLiteHelper(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                finish();
                break;
            case R.id.ivDelete:
                etContent.setText("");
                break;
            case R.id.ivSave:
                String content = etContent.getText().toString().trim();
                if (content.length() > 0) {
                    if (helper.insertData(content, DBUtils.getTime())) {
                        showToast("保存成功");
                        setResult(2);
                        finish();
                    } else {
                        showToast("保存失败");
                    }
                }
                break;
        }
    }
    public void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}